﻿using Coldairarrow.Business.PB;
using Coldairarrow.Entity.DTO;
using Coldairarrow.Entity.PB;
using Coldairarrow.Util;
using Coldairarrow.Util.ClassLibrary;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Coldairarrow.Api.Controllers.PB
{
    [Route("/PB/[controller]/[action]")]
    public class PB_PurchaseController : BaseApiController
    {
        #region DI
        private readonly RedisOptions _redisOptions; //1.声明实体
        private readonly MongoDBOptions _mongodbOptions;
        public PB_PurchaseController(IPB_PurchaseBusiness pB_PurchaseBus, IOptions<MongoDBOptions> mongodbOptions, IOptions<RedisOptions> redisOptions)
        {
            _pB_PurchaseBus = pB_PurchaseBus;
            _mongodbOptions = mongodbOptions.Value;
            _redisOptions = redisOptions.Value;//3.赋值
        }

        IPB_PurchaseBusiness _pB_PurchaseBus { get; }

        #endregion

        #region 获取

        [HttpPost]
        public async Task<PageResult<PB_Purchase>> GetDataList(PageInput<ConditionDTO> input)
        {
            return await _pB_PurchaseBus.GetDataListAsync(input);
        }

        [HttpPost]
        public async Task<PB_Purchase> GetTheData(IdInputDTO input)
        {
            return await _pB_PurchaseBus.GetTheDataAsync(input.id);
        }

        /// <summary>
        /// 外采订单列表
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<PageResult<PB_PurchaseDTO>> GetPurchaseDataList(PageInput<PurchaseConditionDTO> input)
        {
            return await _pB_PurchaseBus.GetPurchaseDataListAsync(input);
        }


        /// <summary>
        /// 外采到货列表
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<PageResult<PB_PurchaseDTO>> GetPurchaseArrivalDataList(PageInput<PurchaseConditionDTO> input)
        {
            return await _pB_PurchaseBus.GetPurchaseArrivalDataListAsync(input);
        }

        /// <summary>
        /// 查看外采订单信息
        /// </summary>
        /// <param name="PurchaseId">外采订单Id</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<PB_PurchaseDataDTO> GetPurchaseData(string PurchaseId)
        {
            return await _pB_PurchaseBus.GetPurchaseDataAsync(PurchaseId);
        }

        /// <summary>
        /// 保存外采订单
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task SavePurchaseData(PurchaseData data)
        {
            await _pB_PurchaseBus.SavePurchaseDataAsync(data);
        }

        /// <summary>
        /// 审核外采订单
        /// </summary>
        /// <param name="PurchaseId">外采订单Id</param>
        /// <returns></returns>
        [HttpPost]
        public async Task ToExaminePurchaseData(string PurchaseId)
        {
            await _pB_PurchaseBus.ToExaminePurchaseDataAsync(PurchaseId);
        }

        /// <summary>
        /// 获取外采订单明细信息
        /// </summary>
        /// <param name="PurchaseId">外采订单Id</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<List<PurchasePrintDTO>> GetPurchaseDetailList(string PurchaseId)
        {
            return await _pB_PurchaseBus.GetPurchaseDetailListAsync(PurchaseId);
        }

        /// <summary>
        /// 外采单确认
        /// </summary>
        /// <param name="PurchaseId"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task DeterminePurchaseData(string PurchaseId)
        {
            await _pB_PurchaseBus.DeterminePurchaseDataAsync(PurchaseId);
        }


        /// <summary>
        /// 外采订单打码
        /// </summary>
        /// <param name="data">外采订单Id</param>
        /// <returns></returns>
        [HttpPost]
        public async Task PrintPurchase(PurchasePrintData data)
        {
            await _pB_PurchaseBus.PrintPurchaseAsync(data, _mongodbOptions, _redisOptions);
        }

        /// <summary>
        /// 外采订单成品码明细
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<PageResult<PB_JobBookingQrCodeDTO>> GetPurchaseDetailJobBookingQrCode(PageInput<PurchaseConditionDTO> input)
        {
            return await _pB_PurchaseBus.GetPurchaseDetailJobBookingQrCodeAsync(input);
        }

        /// <summary>
        /// 成品物料补打
        /// </summary>
        /// <param name="data"></param>
        /// <param name="_mongodbOptions"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task PrintPurchaseJobBookingQrCode(PurchaseMaterialPrintDataDTO data)
        {
            await _pB_PurchaseBus.PrintPurchaseJobBookingQrCodeAsync(data, _mongodbOptions);
        }

        /// <summary>
        /// 外采订单包明细
        /// </summary>
        /// <param name="PurchaseId">外采订单Id</param>
        /// <param name="MaterialId">物料Id</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<List<PurchasePackList>> GetPurchasePackListAsync(string PurchaseId, string MaterialId)
        {
            return await _pB_PurchaseBus.GetPurchasePackListAsync(PurchaseId, MaterialId);
        }

        /// <summary>
        /// 外采订单包码补打
        /// </summary>
        /// <param name="data"></param>
        /// <param name="_mongodbOptions"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task PrintPurchasePackList(PurchaseMaterialPrintDataDTO data)
        {
            await _pB_PurchaseBus.PrintPurchasePackListAsync(data, _mongodbOptions);
        }

        /// <summary>
        /// 外采订单签收明细
        /// </summary>
        /// <param name="PurchaseId"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<PB_PurchaseDataDTO> GetSurplusPurchaseDetail(string PurchaseId)
        {
            return await _pB_PurchaseBus.GetSurplusPurchaseDetailAsync(PurchaseId);
        }

        /// <summary>
        /// 外采订单签收
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task UpdatePurchaseData(PB_PurchaseDataDTO data)
        {
            await _pB_PurchaseBus.UpdatePurchaseDataAsync(data);
        }

        #endregion

        #region 提交

        [HttpPost]
        public async Task SaveData(PB_Purchase data)
        {
            if (data.Id.IsNullOrEmpty())
            {
                InitEntity(data);

                await _pB_PurchaseBus.AddDataAsync(data);
            }
            else
            {
                await _pB_PurchaseBus.UpdateDataAsync(data);
            }
        }

        [HttpPost]
        public async Task DeleteData(List<string> ids)
        {
            await _pB_PurchaseBus.DeleteDataAsync(ids);
        }

        #endregion
    }
}